<script src="/js/jquery-3.0.0.min.js"></script>
<script>
    function edit(id) {
        document.querySelector('#form').action = "/tag/" + id
        document.querySelector('#description').value = document.querySelector('#desc' + id).textContent
        document.querySelector('#name').value = document.querySelector('#name' + id).textContent
        document.querySelector('#externallink').value = document.querySelector('#link' + id).textContent
        $('#form > div:nth-child(5) > span > input').each(function(i, checkbox) {
           $(checkbox).prop('checked', false);
        })
        var list = $('#tasks' + id).attr('list').split(',').forEach(function (taskid) {
            $('#task_id' + taskid).prop('checked', true);
        })
    }
    function update() {

    }
    function del(id) {
        $.get('/tag/' + id + '/delete', function () {
            window.location.href = '/tag'
        })
    }
</script>
<table>
    <tr>
        <th>id</th>
        <th>name</th>
        <th>desc</th>
        <th>link</th>
        <th>related_tasks</th>
    </tr>

    @foreach ($tags as $tag)
        <tr>
            <td id="id{{ $tag->id }}">{{ $tag->id }}</td>
            <td id="name{{ $tag->id }}"><a target="_blank" href="/skill/{{$tag->id}}">{{ $tag->name }}</a></td>
            <td id="desc{{ $tag->id }}">{{ $tag->description }}</td>
            <td id="link{{ $tag->id }}">{{ $tag->externallink }}</td>
            <td id="tasks{{ $tag->id }}" list="{{ $tag->taskids }}">{{ $tag->tasknames }}</td>
            <td>
                <button onclick="edit({{ $tag->id }})">修改</button>
            </td>
            <td>
                <button onclick="del({{ $tag->id }})">删除</button>
            </td>
        </tr>
    @endforeach

</table>

<form method="POST" action="/tag" id="form">
    {!! csrf_field() !!}
    <div class="form-group">
        <div>{!! Form::label('name', 'name:') !!}</div>
        {!! Form::textarea('name', null, array('id' => 'name')) !!}
    </div>

    <div class="form-group">
        <div>{!! Form::label('desc', 'desc:') !!}</div>
        {!! Form::textarea('description', null, array('id' => 'description')) !!}
    </div>

    <div class="form-group">
        <div>{!! Form::label('externallink', 'link:') !!}</div>
        {!! Form::textarea('externallink', null, array('id' => 'externallink')) !!}
    </div>

    <div class="form-group">
        <div>{!! Form::label('related_tasks', 'related_tasks:') !!}</div>
        @foreach ($tasks as $task)
            <span style="padding: 5px;">
                <label style="background-color:{!! isset($covered_tasks[$task->id])?"green":"red" !!}">{{ $task->title }}</label>
                <input type="checkbox" id="task_id{{ $task->id }}" name="{{ $task->id }}">
            </span>
            {{--{!! Form::label({{ $task->id }}, 'link') !!}--}}
            {{--{!! Form::checkbox('externallink') !!}--}}
        @endforeach

    </div>

    <div class="form-group">
        {!! Form::submit("提交", null, array('id' => 'submit')) !!}
    </div>

</form>
